package forcollege.xly.com.xian.viewmodel;

import android.databinding.ObservableField;
import android.widget.Toast;

import forcollege.xly.com.xian.base.BaseViewModel;
import forcollege.xly.com.xian.model.ServiceData;
import forcollege.xly.com.xian.presenter.LoginPresenter;
import forcollege.xly.com.xian.view.MainActivity;
import forcollege.xly.com.xian.vpinterface.LoginInterface;

/**
 * Created by xuleyuan on 2018/8/10
 */

public class MainViewModel extends BaseViewModel<MainActivity> implements LoginInterface {
    private LoginPresenter loginPresenter;
    public final ObservableField<String> account = new ObservableField<>();
    public final ObservableField<String> password = new ObservableField<>();

    public MainViewModel(MainActivity view) {
        super(view);
    }

    @Override
    public void onCreate() {
        loginPresenter = new LoginPresenter();
        loginPresenter.attach(this);
    }

    @Override
    public void onResume() {

    }

    @Override
    public void onDestroy() {
        loginPresenter.detch();
    }

    /**
     * 请求网络数据
     */
    public void doLogin() {
        loginPresenter.putParams("account", account.get())
                .putParams("password", password.get())
                .doRequest();
    }

    /**
     * 当获取数据成功调用刷新UI
     *
     * @param data
     */
    @Override
    public void onLoginSuccess(ServiceData data) {
        Toast.makeText(getView(), "登陆成功", Toast.LENGTH_SHORT).show();
    }
}
